package com.example.demo.service.Impl;

import com.example.demo.entity.Licence;
import com.example.demo.entity.Subject;
import com.example.demo.mapper.SubjectMapper;
import com.example.demo.service.SubjectService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
@RequiredArgsConstructor
@Transactional
public class SubjectImpl implements SubjectService {

    private final SubjectMapper subjectMapper;
    @Override
    public List<Subject> findAllSubject() {
        return subjectMapper.selectList(null);
    }

    @Override
    public Subject addSubject(Subject subject) {
        int result = subjectMapper.insert(subject);
        if(result>0){
            return subject;
        }
        else {
            throw new RuntimeException("Failed to add Subject");
        }
    }

    @Override
    public Long deleteSubject(Long subjectId) {
//        QueryWrapper queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("licence_id",licenceId);
        int result = subjectMapper.deleteById(subjectId);
        if(result>0){
            return subjectId;
        }
        else {
            throw new RuntimeException("Failed to delete Subject");
        }
    }

    @Override
    public Subject updateSubject(Subject subject) {
        int result = subjectMapper.updateById(subject);
        if(result>0){
            return subject;
        }
        else {
            throw new RuntimeException("Failed to update Subject");
        }
    }
}
